package com.example.learndevice.ui

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.hardware.Camera
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.Window
import android.widget.ImageView
import android.widget.Toast
import com.example.learndevice.R
import com.example.learndevice.adapter.ShootingAdapter
import com.example.learndevice.databinding.ActivityPhotographBinding
import com.example.learndevice.widget.CameraView
import com.example.mydefinedview1.base.BaseActivity

class PhotographActivity : BaseActivity<ActivityPhotographBinding>(),View.OnClickListener {

    override fun ActivityPhotographBinding.initBinding() {

        mBinding.btnCatchBehind.setOnClickListener(this@PhotographActivity)
        mBinding.btnCatchFront.setOnClickListener(this@PhotographActivity)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(localClassName,"onActivityResult. requestCode=$requestCode,resultCode=$resultCode")
        val bundle = data?.extras
        val isNull = bundle?.getString("is_null")
        if (isNull != "yes"){
            val type = bundle?.getInt("type")
            Log.d(localClassName,"type=$type")
            if (type == CameraView.CAMERA_BEHIND){
                mBinding.ivPhoto.visibility = View.VISIBLE
                mBinding.gvShooting.visibility = View.GONE
                val path = bundle.getString("path")
                fillBitmap(BitmapFactory.decodeFile(path,null))
            }else if (type == CameraView.CAMERA_FRONT){
                mBinding.ivPhoto.visibility = View.GONE
                mBinding.gvShooting.visibility = View.VISIBLE
                val pathList = bundle.getStringArrayList("path_list")
                val path = bundle.getString("path")
                fillBitmap(BitmapFactory.decodeFile(path,null))
                Log.d(localClassName,"pathList.size() =${pathList?.size}")
                val adapter = ShootingAdapter(pathList!!)
                mBinding.gvShooting.adapter = adapter
            }
        }
    }

    private fun fillBitmap(bitmap: Bitmap){
        Log.d(localClassName,"fillBitmap width=${bitmap.width},height =${bitmap.height}")
        if(bitmap.height > mBinding.flContent.height){
            val params = mBinding.ivPhoto.layoutParams
            params.height = mBinding.flContent.measuredHeight
            params.width = bitmap.width*mBinding.flContent.measuredHeight / bitmap.height
            mBinding.ivPhoto.layoutParams = params
        }
        mBinding.ivPhoto.isDrawingCacheEnabled = true
        mBinding.ivPhoto.scaleType = ImageView.ScaleType.FIT_CENTER
        mBinding.ivPhoto.setImageBitmap(bitmap)
    }

    override fun onClick(v: View?) {
        when(v?.id){
            mBinding.btnCatchBehind.id -> {
                val mCamera = Camera.open()
                if (mCamera != null){
                    mCamera.release()
                    val intent = Intent(this,TakePictureActivity::class.java)
                    intent.putExtra("type",CameraView.CAMERA_BEHIND)
                    startActivityForResult(intent,1)
                }else{
                    Toast.makeText(this,"当前设备不支持后置摄像头",Toast.LENGTH_SHORT).show()
                }
            }
            mBinding.btnCatchFront.id -> {
                Log.d(localClassName,"getNumberOfCameras=${Camera.getNumberOfCameras()}")
                val mCamera = Camera.open(CameraView.CAMERA_FRONT)
                if (mCamera != null){
                    mCamera.release()
                    val intent = Intent(this,TakePictureActivity::class.java)
                    intent.putExtra("type",CameraView.CAMERA_FRONT)
                    startActivityForResult(intent,1)
                }else{
                    Toast.makeText(this,"当前设备不支持前置摄像头",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

}